<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

if(isset($_REQUEST['search'])){
	$circles = $MAIN->circles->searchCircles($_REQUEST['search']);
	foreach($circles as $circle){
	?>
	<div onclick="fillForm('circles',<?php echo($circle['id']);?>);" id="circle_<?php echo($circle['id']);?>"><?php echo($circle['name']);?></div>
	<?php
	}
	?>
	<script type="text/javascript">
	var circles={};
	<?php	
	foreach($circles as $circle){
	?>
	circles[<?php echo($circle['id']);?>]=<?php echo(json_encode($circle));?>;	 
	<?php
	}
	?>
	</script>
	<?php
	exit();
}

if(isset($_POST['txt_circle_name'])){
	if($asId = $MAIN->circles->setCircle($_POST['txt_circle_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=circles&id=<?php echo($asId);?>&view=edit';}).delay(3000);</script>
	<?php
	}
	exit();
}
if(isset($_REQUEST['del'])){
	if($MAIN->circles->delCircle($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=circles&view=all';}).delay(3000);</script>
	<?php	
	};
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
	$ord="ASC";
	$orderBy="name";
	if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
	$nextOrd="DESC";
	if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
	$circles = $MAIN->circles->getCircles($orderBy,$ord);	
?>
	<h1><?php if($MAIN->currentUser['grouplevel']<30){ ?>Alla<?php }else{?>Mina<?php }?> Aktiviteter</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=type&amp;ord=<?php echo($nextOrd);?>">Typ</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=leader&amp;ord=<?php echo($nextOrd);?>">Ledare</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=ass&amp;ord=<?php echo($nextOrd);?>">Förening</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=name&amp;ord=<?php echo($nextOrd);?>">Namn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=topic&amp;ord=<?php echo($nextOrd);?>">Ämne</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=circles&amp;view=all&amp;ordby=start_date&amp;ord=<?php echo($nextOrd);?>">Datum</a></th>
		<th style="width:10px;"></th>		
		<th><a href="?cat=circles&amp;view=all&amp;ordby=city&amp;ord=<?php echo($nextOrd);?>">Ort</a></th>
		<th style="width:10px;"></th>		
		<th>Administrera</th>
	</tr>
	<?php
	foreach($circles as $circle){
	?>
	<tr class="entry">
		<td><?php echo($circle['type']);?></td>
		<td></td>
		<td>
			<?php
			$leader = $MAIN->persons->getPerson($circle['leader']);
			if($leader){
				echo($leader['firstname']." ".$leader['lastname']);
			}			
			?>
		</td>
		<td></td>
		<td>
			<?php
			$ass = $MAIN->associations->getAssociation($leader['association_id']);			
			echo($ass['name']);?>
		</td>
		<td></td>
		<td><?php echo($circle['name']);?></td>
		<td></td>
		<td><?php echo($circle['topic']);?></td>
		<td></td>
		<td><?php echo($circle['start_date']);?></td>
		<td></td>
		<td><?php echo($circle['city']);?></td>
		<td></td>
		<td class="tools">			
			<a href="javascript:void(0);" onclick="delCircle(<?php echo($circle['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>			
			<a href="?cat=circles&amp;view=edit&amp;id=<?php echo($circle['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera" title="Redigera"/></a>
		</td>
	</tr>
	<?php
	}
	?>
	</table>
	<script type="text/javascript">
		var delCircle = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('circles.php','del='+id);
			}
		}
	</script>
<?php
	break;
	case 'add':
	case 'edit':
		$circle = $MAIN->circles->getCircle(@$_REQUEST['id']);
		if(@$circle['start_date']==""){
			$circle['start_date']=date('Y-m-d');
		}
?>
	<h1>Skapa/Redigera</h1>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>
				<form name="frm_circle" id="frm_circle" action="circles.php" method="post" onsubmit="return false;">
				<input type="hidden" name="txt_circle_id" value="<?php echo($circle['id']);?>"/>
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td>Typ</td>
						<td>
							<select name="ddl_circle_type">
							<?php
							foreach($MAIN->circles->circlesType as $type){
							?>
							<option value="<?php echo($type);?>"><?php echo($type);?></option>
							<?php
							}
							?>
							</select>
						</td>
					</tr>
					<tr>
						<td>Namn</td>
						<td><input class="required" type="text" name="txt_circle_name" value="<?php echo(@$circle['name']);?>"/></td>
					</tr>
					<tr>
						<td>Ämne</td>
						<td><input class="required" type="text" name="txt_circle_topic" value="<?php echo(@$circle['topic']);?>"/></td>
					</tr>
					<tr>
						<td>Material</td>
						<td><input class="required" type="text" name="txt_circle_material" value="<?php echo(@$circle['material']);?>"/></td>
					</tr>
					<tr>
						<td>Startdatum</td>
						<td><input class="datepicker required" id="txt_circle_start_date" type="text" name="txt_circle_start_date" value="<?php echo(@$circle['start_date']);?>"/></td>
					</tr>
					<tr>
						<td>Veckodag</td>
						<td><input type="text" readonly="readonly" name="txt_circle_day_of_the_week" id="txt_circle_day_of_the_week" value="<?php echo(@$circle['day_of_the_week']);?>"/></td>
					</tr>
					<tr>
						<td>Starttid</td>
						<td><input class="timepicker required" type="text" name="txt_circle_start" value="<?php echo(@$circle['start']);?>"/></td>
					</tr>
					<tr>
						<td>Sluttid</td>
						<td><input class="timepicker required" type="text" name="txt_circle_end" value="<?php echo(@$circle['end']);?>"/></td>
					</tr>
					<tr>
						<td>Antal träffar(per termin)</td>
						<td><input class="required" type="text" name="txt_circle_number_of_meetings" value="<?php echo(@$circle['number_of_meetings']);?>"/></td>
					</tr>
					<tr>
						<td>Adress(lokal)</td>
						<td><input class="required" type="text" name="txt_circle_place" value="<?php echo(@$circle['place']);?>"/></td>
					</tr>
					<tr>
						<td>Postnummer</td>
						<td><input class="required" type="text" name="txt_circle_zip" value="<?php echo(@$circle['zip']);?>"/></td>
					</tr>
					<tr>
						<td>Ort</td>
						<td><input class="required" type="text" name="txt_circle_city" value="<?php echo(@$circle['city']);?>"/></td>
					</tr>
					<tr>
						<td colspan="2" style="text-align:right;">
							<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_circle');"/>
						</td>
					</tr>
				</table>
				</form>
				<script type="text/javascript">
				window.addEvent('domready', function(){	
					var datePicker = new Picker.Date($$('.datepicker'), {
						positionOffset: {x: 5, y: 0},
						pickerClass: 'datepicker_dashboard'
					});	
					var timePicker = new Picker.Date($$('.timepicker'), {
						pickOnly: 'time',		
						pickerClass: 'datepicker_dashboard'
					});
				});
				$('txt_circle_start_date').addEvent('change',function(){
					var date= new Date().parse(this.value);;
					$('txt_circle_day_of_the_week').value=weekday[date.getDay()];
				});
				</script>
			</td>
			<td>
				<div class="notes">
					<?php 
					if(isset($_REQUEST['id'])&&$_REQUEST['id']!=0){
					?>
					<h2 style="text-align:right;">Allmänna notiser</h2>
					<div class="note" style="width:410px; text-align:right;float:right;">
						<textarea name="txt_note" id="txt_note" rows="" cols=""></textarea>
						<div class="tools" style="padding:0px;">
							<input type="submit" name="btn_submit" value="Spara" onclick="ajaxGet('note.php','rel_table=circles&id=<?php echo($circle['id']);?>&text='+$('txt_note').value);$('txt_note').value='';"/>
						</div><br/>
					</div>				
					<div class="cnt_notes" id="cnt_notes">
					</div>
					<script type="text/javascript">
					ajaxGet('notes.php','rel_table=circles&rel_id=<?php echo($circle['id']);?>',$('cnt_notes'));
					</script>
					<?php }?>
						
				</div>				
			</td>
		</tr>		
	</table>
<?php
	break;
}
?>
</div>